# -*- coding:utf-8 -*-

import csv
from pymongo import MongoClient

client = MongoClient('127.0.0.1', 27017)
db = client["Medical"]
table = db["aaa_2020年税务师（全套五科）考试题库ID:505"]
# datas = table.find({"$and":[{"type": ""}, {"title": ""}]})
datas = table.find()
line = 0
previous = ""
for d in datas:
    dict_data = dict()
    dict_data['标题'] = d.get('product_info').replace("（", "(").replace("）", ")")
    dict_data['科目'] = d.get('kemu')
    dict_data['类型'] = d.get('part_type')
    dict_data['章'] = d.get('part')
    dict_data['二级章'] = d.get('name')
    dict_data['题型'] = d.get('type_name')
    dict_data['主题号'] = d.get('pid')
    dict_data['次题号'] = d.get('item_id')
    dict_data['题干'] = d.get('main_title')
    dict_data['问题'] = d.get('title')
    dict_data['题干问题'] = d.get('title_tmp')
    dict_data['参考答案'] = d.get('analysis')
    dict_data['答案'] = d.get('answerContent').replace(" ", "")

    if d.get('content_1') is not None:
        dict_data['选项A'] = d.get('content_1').split(".")[1]
    else:
        dict_data['选项A'] = ""
    if d.get('content_2') is not None:
        dict_data['选项B'] = d.get('content_2').split(".")[1]
    else:
        dict_data['选项B'] = ""
    if d.get('content_3') is not None:
        dict_data['选项C'] = d.get('content_3').split(".")[1]
    else:
        dict_data['选项C'] = ""
    if d.get('content_4') is not None:
        dict_data['选项D'] = d.get('content_4').split(".")[1]
    else:
        dict_data['选项D'] = ""
    if d.get('content_5') is not None:
        dict_data['选项E'] = d.get('content_5').split(".")[1]
    else:
        dict_data['选项E'] = ""
    if d.get('content_6') is not None:
        dict_data['选项F'] = d.get('content_6').split(".")[1]
    else:
        dict_data['选项F'] = ""
    if d.get('content_7') is not None:
        dict_data['选项G'] = d.get('content_7').split(".")[1]
    else:
        dict_data['选项G'] = ""
    if d.get('content_8') is not None:
        dict_data['选项H'] = d.get('content_8').split(".")[1]
    else:
        dict_data['选项H'] = ""
    if d.get('content_9') is not None:
        dict_data['选项I'] = d.get('content_9').split(".")[1]
    else:
        dict_data['选项I'] = ""
    # dict_data['选项A'] = d.get('content_1').split(".")[1]
    # dict_data['选项B'] = d.get('content_2').split(".")[1]
    # dict_data['选项C'] = d.get('content_3').split(".")[1]
    # dict_data['选项D'] = d.get('content_4').split(".")[1]
    # dict_data['选项F'] = d.get('content_6').split(".")[1]
    # dict_data['选项G'] = d.get('content_7').split(".")[1]
    # dict_data['选项H'] = d.get('content_8').split(".")[1]
    # dict_data['选项I'] = d.get('content_9').split(".")[1]
    print(str(dict_data))
    if dict_data['二级章'] != "":
        path = "./csv/" + dict_data['科目'].strip() + "_" + dict_data['章'].strip() + "_" + dict_data['二级章'].strip() + ".csv"


    if previous != path:
        previous = path
        with open(path, "a+", encoding="utf-8-sig", newline="") as f:
            csv_writer = csv.writer(f)
            csv_writer.writerow(["标题", "科目", "类型", "章", "二级章", "题型", "主题号", "次题号",
                                 "题干", "问题", "题干问题", "参考答案", "答案", "选项A", "选项B", "选项C", "选项D",
                                 "选项E", "选项F", "选项G", "选项H", "选项I"])


    with open(path, "a", encoding="utf-8-sig", newline="") as csvfile:
        fileheader = ["标题", "科目", "类型", "章", "二级章", "题型", "主题号", "次题号",
                      "题干", "问题", "题干问题", "参考答案", "答案", "选项A", "选项B", "选项C", "选项D",
                      "选项E", "选项F", "选项G", "选项H", "选项I"]
        writer = csv.DictWriter(csvfile, fieldnames=fileheader)
        writer.writerow(dict_data)
    line += 1
    print(line)